# -*- encoding=utf8 -*-

import time

from apicore.PocoApi import PocoApi
from base_page.phone_home.Phone_Home import Phone_Home

""" 备份恢复 """


class BackupAndRestore(PocoApi):
    def __init__(self):
        super().__init__()
        self.phone_home = Phone_Home()

    # 进入一键安装
    def enter_page(self):
        self.phone_home.backup_and_restore()

    def cloudphone_backups(self):
        self.enter_page()
        self.phone_home.poco_click(text="云机备份")
        self.phone_home.poco_click(text="确认")
        self.wait_copy_status()

    def cloudphone_restore(self):
        self.enter_page()
        self.phone_home.poco_click(text="云机恢复")
        self.phone_home.poco_click(text="从备份恢复")
        self.wait_recover_status()

    # 备份状态
    def wait_copy_status(self):
        for i in range(300):
            element = self.poco(name="com.unicom.cloudphone:id/tv_status")
            time.sleep(1)
            if element.exists() and element.get_text() == "备份中":
                print("备份中")
                continue
            if element.exists() and element.get_text() == "运行中":
                print("运行中")
                break
            else:
                print("云机备份失败")
        # # 等待 "备份中" 状态
        # start_time = time.time()
        # while time.time() - start_time < 120:
        #     status_element = self.poco(resourceId="com.unicom.cloudphone:id/tv_status")
        #     if status_element.exists() and status_element.get_text() == "备份中":
        #         print("云机正在备份中")
        #         break
        #     time.sleep(1)
        # else:
        #     print("云机状态失败")
        # # 等待 "运行中" 状态
        # start_time = time.time()
        # while time.time() - start_time < 60:
        #     status_element = self.poco(resourceId="com.unicom.cloudphone:id/tv_status")
        #     if status_element.exists() and status_element.get_text() == "运行中":
        #         print("云机备份成功")
        #         break
        #     time.sleep(1)
        # else:
        #     print("云机备份失败")

    # 恢复状态
    def wait_recover_status(self):
        for i in range(300):
            element = self.poco(name="com.unicom.cloudphone:id/tv_status")
            time.sleep(1)
            if element.exists() and element.get_text() == "恢复中":
                print("恢复中")
                continue
            if element.exists() and element.get_text() == "运行中":
                print("恢复状态完成")
                break
            else:
                print("云机恢复失败")
        #
        # # 等待 "备份中" 状态
        # start_time = time.time()
        # while time.time() - start_time < 120:
        #     status_element = self.poco(resourceId="com.unicom.cloudphone:id/tv_status")
        #     if status_element.exists() and status_element.get_text() == "恢复中":
        #         print("云机正在恢复中")
        #         break
        #     time.sleep(1)
        # else:
        #     print("云机状态失败")
        # # 等待 "运行中" 状态
        # start_time = time.time()
        # while time.time() - start_time < 60:
        #     status_element = self.poco(resourceId="com.unicom.cloudphone:id/tv_status")
        #     if status_element.exists() and status_element.get_text() == "运行中":
        #         print("云机恢复成功")
        #         break
        #     time.sleep(1)
        # else:
        #     print("云机恢复失败")
